package gudaziEx.entity;

import fgoScript.entity.BaseZButton;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class MyComboBoxUI extends BasicComboBoxUI {
//    private ImageIcon DOWN_ICON = new ImageIcon(getClass().getClassLoader().getResource("down.png"));

    private static Color DEFAULT_COLOR = new Color(50, 50, 50);
    private static Color DEFAULT_COLOR02 = new Color(150, 150, 150);


    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        this.arrowButton.setPreferredSize(new Dimension(15,15));
        JComboBox comboBox = (JComboBox) c;
        comboBox.setFocusable(true);
        comboBox.setOpaque(false);
        comboBox.setRenderer(new MyListCellRenderer());
    }

    @Override
    protected JButton createArrowButton() {
        BaseZButton bt =  new BaseZButton(3, "▲");
//        bt.setMargin(new Insets(80, 80, 80, 80));
//        bt.setUI(new MyButtonUI());
        bt.setSize(new Dimension(15,15));
        return bt;
    }

    @Override
    protected LayoutManager createLayoutManager() {
        return super.createLayoutManager();
    }

    @Override
    public void configureArrowButton() {
        super.configureArrowButton();
    }
    //    @Override
//    protected JButton createArrowButton() {
//        // 也可以使用BasicComboBoxUI里的arrowButton对象
//        JButton arrow = new JButton();
//        // 设置自己定义的UI
//        arrow.setUI(new MyButtonUI());
////        // 设置图标
////        arrow.setIcon(DOWN_ICON);
//        // 设置无法获得焦点
//        arrow.setFocusable(false);
//        // 设置边距，调整图标位置
//        arrow.setMargin(new Insets(0, 20, 0, 0));
//        return arrow;
//    }

    @Override
    public void paint(Graphics g, JComponent c) {

        // 也可以使用BasicComboBoxUI里的combobox对象
        JComboBox comboBox = (JComboBox) c;
        hasFocus = comboBox.hasFocus();
//        comboBox.setSize(new Dimension(80,25));

        Rectangle r = rectangleForCurrentValue();
        // JComboBox的textfield的绘制,并不是靠Renderer来控制
        // 它会通过paintCurrentValueBackground来绘制背景
        // 然后通过paintCurrentValue去绘制显示的值
        RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0,
                comboBox.getWidth() - 3, comboBox.getHeight() - 3, 4, 4);
        Graphics2D g2d = (Graphics2D) g;
        g2d.clip(r2d);
        // 边框透明度
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.65f));
        // 获取焦点时，用不同颜色来区分
        if (comboBox.hasFocus()) {
            g2d.setColor(DEFAULT_COLOR);
        } else {
            g2d.setColor(DEFAULT_COLOR02);
        }
        if (!comboBox.isEditable()) {
            paintCurrentValueBackground(g2d, r, hasFocus);
            paintCurrentValue(g2d, r, hasFocus);
        } else {
            paintCurrentValueBackground(g2d, r, hasFocus);
        }
    }

    @Override
    protected ComboPopup createPopup() {
        BasicComboPopup popup = (BasicComboPopup) super.createPopup();
        // 获取到popup，为其设置边框，和combobox的颜色保持同步
        popup.setBorder(BorderFactory.createLineBorder(DEFAULT_COLOR));
        return popup;
    }
}
